import pandas as pd
import datetime
from ..model.zhimas import Zhimas
from ..model.tags import Tags
from ..model.users import Users
from ..calculate.core import *
from bson.objectid import ObjectId
from abc import ABCMeta


class UpdateTag(object):
    __metaclass__ = ABCMeta

    def __init__(self, dic):
        print(dic)
        self.zhimas = Zhimas()
        self.tags = Tags()
        self.users = Users()
        self.user_id = dic.get('userId', None)
        self.loan_id = dic.get('loanId', None)
        self.tags_list = []

    def update_tag(self):
        self.tag_detail()
        self.zhimas_score()
        tags_detail = pd.DataFrame(self.tags_detail)
        tags_name = pd.DataFrame({'name': self.tags_list})
        data = tags_detail.merge(tags_name).to_dict('records') # 标签详细信息 list
        self.users.update_one({'_id': ObjectId(self.user_id)}, {'tags': data})

        return


    def tag_detail(self):
        self.tags_detail = self.tags.find_detail()

    def zhimas_score(self):
        '''
        芝麻信用分
        :return:
        '''
        data = self.zhimas.find_score({'userId': self.user_id})
        tags = zhimas_score_tag(float(data[0]['score']))
        self.tags_list.extend(tags)